<?php

App::uses('AppModel', 'Model');

class KalkulatorOsiguranje extends AppModel {
    
    public $useTable = 'kalkulator_osiguranje';
    
    public function calculate($data) {
        $value = 0;
        
        switch ($data['vrsta_vozila']) {
            default:
                $refValue = (float)$data['snaga'];
                break;
            
            case 'teretno':
                $refValue = (float)$data['nosivost'];
                break;
            
            case 'motorcikl':
                $refValue = (float)$data['zapremina'];
                break;
        }
        $row = $this->find('first', array(
            'fields'     => array('value'),
            'conditions' => array(
                'vrsta_vozila' => $data['vrsta_vozila'],
                'min <'        => $refValue,
                'max >='       => $refValue
            )
        ));
        if ($row) {
            $value = (float)$row[$this->name]['value'];
        }
        
        // premijski stepen
        $ps = $data['premijski_stepen'];
        $bs = $data['broj_steta'];
        $ps = $ps ? $ps : 4;
        if ($ps > 0) {

            if ($bs > 0) {
                $ps += 3*$bs;
            } else if ($ps > 1) {
                $ps -= 1;
            }

            $ps = $ps>12 ? 12 : ($ps<1 ? 1 : $ps);

            switch($ps) {
                case  1: $index = 0.85; break;
                case  2: $index = 0.90; break;
                case  3: $index = 0.95; break;
                case  4: $index = 1; break;
                case  5: $index = 1.15; break;
                case  6: $index = 1.30; break;
                case  7: $index = 1.50; break;
                case  8: $index = 1.70; break;
                case  9: $index = 1.90; break;
                case 10: $index = 2.10; break;
                case 11: $index = 2.30; break;
                case 12: $index = 2.50; break;

                default:
                    $index = 1;
            }
            $value *= $index;
        }
        
        switch ($data['vrsta_vozila']) {
            default:
                // TAXI + 20%
                if ($data['taxi']) {
                    $value *= 1.2;
                }
                
                // Rent a Car + 40%
                if ($data['rent_a_car']) {
                    $value *= 1.4;
                }
                
                // Invalid - 10%
                if ($data['invalid']) {
                    $value *= 0.9;
                }
                break;
        }
        
        return $value;
    }
    
}
